<script type="text/ecmascript-6">
  import Navbar from '../../components/base/navbar.vue';
  export default{
    name:"coupon-list",
    components:{
      Navbar
    },
    data(){
         return{
              tabList:[
                {
                    id:0,
                    name:"未使用",
                    page: 0,
                    hasMore: true,
                    init: false,
                    isList:false
                },
                {
                    id:1,
                    name:"已使用",
                    page: 0,
                    hasMore: true,
                    init: false,
                    isList:false
                },
                {
                    id:2,
                    name:"已过期",
                    page: 0,
                    hasMore: true,
                    init: false,
                    isList:false
                }
              ],
              dataList:{

              },
              tabIndex: 2,
              isList:false,
              heights:"100%",
              isDrag:false
         }
    },
    methods:{
      loadMore(){
         var page = this.tabList[this.tabIndex].page + 1;
         this.$store.dispatch("queryCouponList",{index:this.tabIndex,page:page});
      },
      done() {
        this.$nextTick(() => {
//          this.$refs.pulldown[0].onLoadOff();
//          this.$refs.pulldown[1].onLoadOff();
//          this.$refs.pulldown[2].onLoadOff();
          this.$refs.pulldown[this.tabIndex].onLoadOff();
        })
      },
      refresh(){
         this.$store.dispatch("queryCouponList",{index:this.tabIndex});
      },
      jump(page, id) {
         this.$router.push({ name: page, params: {id: id} });
      },
      couponList(res,index){
          let list,
              page=res.meta.pagination,
              current_page =page.current_page,
              total_pages =page.total_pages;
          if( current_page ==1){
              list=res.data;
          }
          else{
              list = this.dataList[index].concat(res.data);
          }
          this.tabIndex=index;
          this.$set(this.dataList,index,list);


          let tabList = Object.assign({},this.tabList[index],{
            init:true,
            page:current_page,
            hasMore:current_page < total_pages
          });

          this.$set(this.tabList,index,tabList)


          if( list.length == 0 ){
            this.tabList[index].isList = true;
          }
          this.$emit('done');
      },
      change(i){
//          console.log(i);
         this.tabIndex=i;
         this.$router.replace({name: 'coupon_list', params: {index: this.tabIndex}});
         if(!this.tabList[i].init){
             this.$store.dispatch('queryCouponList',{index:this.tabIndex})
         }
         else{
             this.isList=!!!this.dataList[i].length;
         }
      }
    },
    created(){
      this.$on('done', this.done);
//       EventBus.
      EventBus.$on("coupon-list",this.couponList);
      this.tabIndex = Number(this.$route.params.index) || 0;
      this.$store.dispatch("queryCouponList",{index:this.tabIndex});

    },
    beforeDestroy(){
      EventBus.$off("coupon-list");
    },
    mounted(){

    }
  }
</script>
